var flag=0;
var pageNo=1;
var pageSize=10;
var searchStatus=-1;
var cityName="";
showList();
var searchName="";
var pageNum="";
keyEnter(serchResult);
//table 的ajax请求
function showList(){
    if(flag!==0){
        return;
    }
    flag=1;
    searchName=$("#searchContent").val();
    ajax({
        url:"airportInfoMain/queryAirportInfoList",
        data:{
            //搜索名
            searchName:searchName,
            //城市名称
            cityName:cityName,
            //查询机场参数
            searchStatus:searchStatus,
            //第几页
            pageNo:pageNo,
            //页面大小
            pageSize:pageSize
        },
        successx:function(data){
            flag=0;
            if(data.success){
                temp(data);
                createPageInfo(data.data.infoList);
                $('.i-checks').iCheck({
                    radioClass: 'iradio_square-green'
                });
            }
        }
    })
}

//关联
$("#submit").click(function(){
    chooseAirport();
});

//搜索
$('#searchName').click(function(){
     pageNo=1;
     showList();
});
function serchResult() {
    pageNo=1;
    showList();
}

//取消选中
$("#cancelConnect").click(function(){
    $('.i-checks').iCheck('uncheck');
});

//按键颜色
$(".btnStyle").click(function(){
    $(".btnStyle").removeClass("btnColor");
    $(this).addClass("btnColor");
});

//选择被选中的机场
function chooseAirport(){
    var checked=$('div.iradio_square-green.checked')[0];
    if(!checked){
        delete window.localStorage.airportConnect;
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }else{
        var td=checked.parentNode.parentNode.parentNode;
        var aiId=$(td.parentNode).attr("data-airportId");
        var aiName=$(td.nextElementSibling).html();
        var airportConnect={"aiId":aiId,"aiName":aiName};
        setStorJson("airportConnect",airportConnect);
        parent.parent.$("#airportName").text(aiName);
        ajax({
            url: "userInfo/updateUserAirportId",
            data: {
                airportId: aiId
            },
            successx: function (data) {
                if (data.success === true) {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                } else {
                    alert(data.message)
                }
            }
        })
    }
}

//改变页面显示数量
$("#pageSizeInfo").change(function () {
    $("#pageSizeInfo").blur()
    if (flag == 1) {
        return
    }
    pageSize = this.options[this.options.selectedIndex].value;
    showList();
});

//城市列表请求接口
ajax({
    url:"airportInfoMain/queryAirportInfoCity",
    data:{

    },
    successx:function(data) {
        createArea(data);
    }
});


// 创建城市列表
function createArea(data){
    var html=template("cityTemplate",data);
    document.getElementById('place').innerHTML = html;
    $('#place').change(function(){
        cityName=$(this).val();
    });
    $('#confirm').click(function(){
       showList();
    })
}

//请求到数据后的table列表
function temp(data){
    var count=data.data.statistics;
    $('#allAirportCount').html(count.allAirportCount);
    $('#contractAirportCount').html(count.contractAirportCount);
    $("#contractNoAirportCount").html(count.contractNoAirportCount);
    $("#hotAirportCount").html(count.hotAirportCount);
    var infoData=data.data.infoList;
    var html=template("template",infoData);
    document.getElementById('tbody').innerHTML = html;
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
    if(getStorJson("airportConnect")!=null){
        $(".radio").each(function (i, d) {
            if($(d).attr("data-airportId")==getStorJson("airportConnect").aiId){
                $(this).iCheck('check')
            }
        })
    }

}

////审核状态
$('.verify>a').on("click",function(){
    if($(this).hasClass("allAirportCount")){
        searchStatus=-1;
    } else if($(this).hasClass("contractAirportCount")){
        searchStatus=1;
    } else if($(this).hasClass("contractNoAirportCount")){
        searchStatus=0;
    } else if($(this).hasClass("hotAirportCount")){
        searchStatus=2;
    }
    pageNo=1;
    showList();
});

//创建页码信息
function createPageInfo(data) {
    $("#startRow").text(data.startRow);
    $("#endRow").text(data.endRow);
    $("#totalPage").text(data.total);
    pageNum=data.pages;
    var temp = '';
    for (var i = 1; i < data.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
        .val(data.pageNum)
        .change(function () {
            $("#pageInfor").blur();
            if (flag == 1) {
                return
            }
            pageNo = this.options[this.options.selectedIndex].value;
            showList();
        });
    $("#firstPage").click(function () {
        if (pageNo!= 1) {
            if (flag == 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < pageNum) {
            if (flag == 1) {
                return
            }
            pageNo = pageNum;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < pageNum) {
            if (flag == 1) {
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag == 1) {
                return
            }
            pageNo--;
            showList();
        }
    })

}